LIMIT = 10000000
def getprimes(limit):
	primes = set()
	nums = range(2,limit+1)
	marked = [0] * limit
	p = 2
	for p in nums:
		if not marked[p-2]:
			n = 2
			a = p*n
			while(a <= limit):
				marked[a-2] = 1
				n = n + 1
				a = p*n
	for x in range(limit-1):
		if not marked[x]:
			primes.add(nums[x])
	return primes
ones = [2,3,5,7]
primes = getprimes(LIMIT)

l = set()
r = set()

def check(num):
    for x in range(1,10):
        current = int(str(x)+str(num))
        if current in primes:
            l.add(current)
            check(current)
def checkr(num):
    for x in range(1,10):
        current = int(str(num)+str(x))
        if current in primes:
            r.add(current)
            checkr(current)

for x in ones:
    check(x)
    checkr(x)
s = l.intersection(r)
print(s)
print("Sum: "+str(sum(s)))
